package cn.stylefeng.guns.sys.modular.system.db2.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.db2.entity.DbiShop;
import cn.stylefeng.guns.sys.modular.system.db2.mapper.DbiShopMapper;
import cn.stylefeng.guns.sys.modular.system.db2.model.params.DbiShopParam;
import cn.stylefeng.guns.sys.modular.system.db2.model.result.DbiShopResult;
import cn.stylefeng.guns.sys.modular.system.db2.service.DbiShopService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-10-27
 */
@Service
public class DbiShopServiceImpl extends ServiceImpl<DbiShopMapper, DbiShop> implements DbiShopService {

    @Resource
    private DbiShopMapper dbiShopMapper;


    @Override
    public void add(DbiShopParam param){
        DbiShop entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DbiShopParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DbiShopParam param){
        DbiShop oldEntity = getOldEntity(param);
        DbiShop newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DbiShopResult findBySpec(DbiShopParam param){
        return null;
    }

    @Override
    public List<DbiShopResult> findListBySpec(DbiShopParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DbiShopParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(DbiShopParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DbiShop getOldEntity(DbiShopParam param) {
        return this.getById(getKey(param));
    }

    private DbiShop getEntity(DbiShopParam param) {
        DbiShop entity = new DbiShop();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public ResponseData shopList() {
//        DbiShopParam shop = new  DbiShopParam();
        List<DbiShopResult> results = dbiShopMapper.listShopNormal();
        return ResponseData.success(results);
    }
}
